package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.CreditCardRewardRecords;
import org.dromara.system.domain.bo.CreditCardRewardRecordsBo;
import org.dromara.system.domain.vo.CreditCardRewardRecordsVo;
import org.dromara.system.domain.vo.CreditCardRewardVo;
import org.dromara.system.mapper.CreditCardRewardRecordsMapper;
import org.dromara.system.service.ICreditCardRewardRecordsService;
import org.dromara.system.service.impl.permission.PermissionBaseService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 刷卡奖励Service业务层处理
 *
 * @author Lion Li
 * @date 2025-07-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class CreditCardRewardRecordsServiceImpl extends ServiceImpl<CreditCardRewardRecordsMapper, CreditCardRewardRecords>
    implements ICreditCardRewardRecordsService {

    private final PermissionBaseService permissionBaseService;


    /**
     * 查询刷卡奖励
     *
     * @param id 主键
     * @return 刷卡奖励
     */
    @Override
    public CreditCardRewardRecordsVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询刷卡奖励列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 刷卡奖励分页列表
     */
    @Override
    public TableDataInfo<CreditCardRewardRecordsVo> queryPageList(CreditCardRewardRecordsBo bo, PageQuery pageQuery) {
        permissionBaseService.assignPermission(bo);
        IPage<CreditCardRewardRecordsVo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        IPage<CreditCardRewardRecordsVo> list = baseMapper.findAll(page, bo);
        TableDataInfo<CreditCardRewardRecordsVo> cardRewardRecordsVoTableDataInfo = TableDataInfo.build(list);
        CreditCardRewardVo creditCardRewardVo = baseMapper.findStatistics(bo);
        cardRewardRecordsVoTableDataInfo.setObj(creditCardRewardVo);
        return cardRewardRecordsVoTableDataInfo;
    }

    @Override
    public TableDataInfo<CreditCardRewardRecordsVo> listReception(CreditCardRewardRecordsBo bo, PageQuery pageQuery) {
        permissionBaseService.aloneAssignPermission(bo);
        IPage<CreditCardRewardRecordsVo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        IPage<CreditCardRewardRecordsVo> list = baseMapper.findAll(page, bo);
        return TableDataInfo.build(list);
    }

    /**
     * 查询符合条件的刷卡奖励列表
     *
     * @param bo 查询条件
     * @return 刷卡奖励列表
     */
    @Override
    public List<CreditCardRewardRecordsVo> queryList(CreditCardRewardRecordsBo bo) {
        LambdaQueryWrapper<CreditCardRewardRecords> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CreditCardRewardRecords> buildQueryWrapper(CreditCardRewardRecordsBo bo) {
        LambdaQueryWrapper<CreditCardRewardRecords> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(CreditCardRewardRecords::getId);
        lqw.eq(bo.getUserId() != null, CreditCardRewardRecords::getUserId, bo.getUserId());
        lqw.eq(bo.getMachineId() != null, CreditCardRewardRecords::getMachineId, bo.getMachineId());
        lqw.eq(StringUtils.isNotBlank(bo.getSn()), CreditCardRewardRecords::getSn, bo.getSn());
        lqw.eq(bo.getCashBackPoints() != null, CreditCardRewardRecords::getCashBackPoints, bo.getCashBackPoints());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), CreditCardRewardRecords::getOrderNo, bo.getOrderNo());
        return lqw;
    }

    /**
     * 新增刷卡奖励
     *
     * @param bo 刷卡奖励
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CreditCardRewardRecordsBo bo) {
        CreditCardRewardRecords add = MapstructUtils.convert(bo, CreditCardRewardRecords.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改刷卡奖励
     *
     * @param bo 刷卡奖励
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CreditCardRewardRecordsBo bo) {
        CreditCardRewardRecords update = MapstructUtils.convert(bo, CreditCardRewardRecords.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CreditCardRewardRecords entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除刷卡奖励信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
